#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include "Usage.hpp"


int main(int argc, char* argv[])
{
    if (argc < 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    //创建套接字
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);
    struct sockaddr_in des;
    bzero(&des, sizeof(des));
    des.sin_family = AF_INET;
    des.sin_port = htons(port);
    des.sin_addr.s_addr = inet_addr(ip.c_str());
    socklen_t len = sizeof(des);
    //发送消息
    while (true)
    {
        std::cout << "Please enter message : " << std::endl;
        char buffer[1024];
        std::cin >> buffer;
        sendto(sockfd, buffer, sizeof(buffer) + 1, 0, (struct sockaddr*)&des, len);
        ssize_t n = recvfrom(sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&des, &len);
        std::cout << "server say : " << buffer << std::endl;
    }
    return 0;
}